'use strict'

var initDB = require('./utls/initDB');

var task = require('./models/task');

var Crawler = require("crawler");

for(var i = 0 ; i < 200 ; i += 1){

    var URL ='https://search.51job.com/list/040000,000000,0000,01,9,99,%2520,2,' + i +'.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=';

    var c = new Crawler({
        maxConnections : 10,
        // 将为每个抓取的页面调用此命令
        callback : function (error, res, done) {
            if(error){
                console.log(error);
            }else{
                //默认的查找库
                var $ = res.$;
                //$默认为cheerio
                //专为服务器控制台设置的核心Jquery
                var list = $(' .dw_table div[class = el]').toArray();
                list.forEach(element => {
                    //.trim()删除空格,.replace正则：
                    var positionName=$(element).find(' .t1 ').text().trim().replace(/[\n\r]/g,'');
                    var companyName = $(element).find(' .t2 ').text();
                    var Workingplace = $(element).find(' .t3 ').text();
                    var Salary = $(element).find(' .t4 ').text();
                    var time = $(element).find(' .t5 ').text();
                    task.create({positionName:positionName,companyName:companyName,Workingplace:Workingplace,Salary:Salary,time:time});
                });
                //console.log(list);
                // console.log($("title").text());
            }
            done();
        }
    });
    c.queue(URL);
}
